<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\member\controller;

class IndexMember extends \app\ApiRest
{
    protected $model;
    protected $rights_model;
    protected $coach_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\member\model\Level();
        $this->rights_model = new \app\member\model\Rights();
        $this->coach_model = new \app\massage\model\Coach();
        $this->rights_model->initData($this->_uniacid);
    }
    public function configInfo()
    {
        $input = $this->_param;
        $config_model = new \app\member\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        return $this->success($config);
    }
    public function index()
    {
        $input = $this->_param;
        $config_model = new \app\member\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $dis[] = ["status", "=", 1];
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $data["data"] = $this->model->where($dis)->order("growth,id desc")->select()->toArray();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                foreach ($v["rights"] as &$vs) {
                    if($vs["key"] == "send_coupon") {
                        $vs["total_discount"] = array_sum(array_column($v["coupon"], "total_discount"));
                    }
                }
            }
        }
        $data["data"] = array_values($data["data"]);
        $data["growth_value"] = $config["growth_value"];
        $data["growth_name"] = $config["growth_name"];
        $data["user_member"] = $this->model->getUserLevel($this->_user["id"]);
        $data["user_member"] = !empty($data["user_member"]) ? $data["user_member"]["id"] : 0;
        $user_model = new \app\massage\model\User();
        $data["growth"] = $user_model->where(["id" => $this->_user["id"]])->value("growth");
        return $this->success($data);
    }
    public function growthList()
    {
        $growth_model = new \app\member\model\Growth();
        $dis[] = ["user_id", "=", $this->_user["id"]];
        $data = $growth_model->dataList($dis, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["growth"] = round($v["growth"]);
                $year = date("Y", $v["create_time"]);
                $month = date("m", $v["create_time"]);
                $v["month_text"] = $year . "年" . $month . "月";
                $v["month"] = date("Y-m", $v["create_time"]);
                $v["create_time"] = date("Y.m.d H:i:s", $v["create_time"]);
                $v["total_growth"] = $growth_model->where($dis)->where(["is_add" => 1])->whereMonth("create_time", $v["month"])->sum("growth");
                $del_growth = $growth_model->where($dis)->where(["is_add" => 0])->whereMonth("create_time", $v["month"])->sum("growth");
                $v["total_growth"] = round($v["total_growth"] - $del_growth);
                $v["total_count"] = $growth_model->where($dis)->where(["is_add" => 1])->whereMonth("create_time", $v["month"])->count();
            }
        }
        $user_model = new \app\massage\model\User();
        $data["growth"] = $user_model->where(["id" => $this->_user["id"]])->value("growth");
        return $this->success($data);
    }
    public function rightsInfo()
    {
        $input = $this->_param;
        $service_model = new \app\member\model\Service();
        $rights = $this->rights_model->dataInfo(["id" => $input["rights_id"]]);
        $level = $this->model->dataInfo(["id" => $input["id"]]);
        $dis = ["a.status" => 1, "b.rights_id" => $input["rights_id"]];
        $list = $this->model->alias("a")->join("massage_member_rights_connect b", "a.id = b.level_id")->where($dis)->field("a.*")->group("a.id")->order("a.growth,a.id desc")->select()->toArray();
        if($rights["key"] == "send_coupon") {
            $rights["coupon"] = $level["coupon"];
            $rights["total_discount"] = array_sum(array_column($rights["coupon"], "total_discount"));
        }
        if(!empty($list)) {
            foreach ($list as &$value) {
                if($rights["key"] == "send_coupon") {
                    $value["total_discount"] = array_sum(array_column($value["coupon"], "total_discount"));
                } elseif($rights["key"] == "appoint_service") {
                    $dis = ["b.status" => 1, "b.member_service" => 1, "a.level_id" => $value["id"]];
                    $value["service_count"] = $service_model->alias("a")->join("massage_service_service_list b", "a.service_id = b.id")->where($dis)->group("b.id")->count();
                }
            }
        }
        $data["rights_info"] = $rights;
        $data["level_list"] = $list;
        return $this->success($data);
    }
}

?>